﻿
using AutoMapper;
using MiniErp.Common.DB.MySqlDB;
using MiniErp.Entity.DataStreams.BOs;
using MiniErp.Entity.DataStreams.DOs.AssetManagement;
using MiniErp.Entity.DataStreams.DTOs.AssetManagement;
using MiniErp.Repository.BaseRepository;
using MiniErp.Repository.MysqlRepository.IRepository.AssetManagement;
using MiniErp.Repository.UnitOfWorkCore;
using MiniErp.Service.JWTService.IService;
using MiniErp.Service.BusinessService.IService.IAssetManagement;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MiniErp.Service.BusinessService.Service.AssetManagement
{
    public class AssetIndexService : BaseService, IAssetIndexService
    {
        private readonly IAssetInfoRepository _air;
        private readonly ITokenService _tokenService;
        private readonly BaseRepository<MysqlContext, AssetModificationType> _amr;
        private readonly IAssetModificationRecordRepository _amrr;
        public AssetIndexService(IMysqlUnitOfWork mysqlUnitOfWork,
            IMapper mapper,
            IAssetInfoRepository air,
            ITokenService tokenService,
            IAssetModificationRecordRepository amrr)
            : base(mysqlUnitOfWork, mapper)
        {
            _air = air;
            _tokenService = tokenService;
            _amr = mysqlUnitOfWork.CreateRepository<MysqlContext, AssetModificationType>();
            _amrr = amrr;
        }

        public async Task<bool> AddAssetInfoAsync(AddAssetInfoDTO addassetInfoDTO)
        {
            var assetInfo = _mapper.Map<AssetInfo>(addassetInfoDTO);
            var maxaid = await _air.GetMaxAssetInfoAIdAsync(assetInfo.GeneralTypeId, assetInfo.SecondaryTypeId);
            //自动生成aid和allid
            assetInfo.AId = maxaid == null ? 100001 : (int)maxaid + 1;
            assetInfo.AllId = assetInfo.GeneralTypeId.ToString() + assetInfo.SecondaryTypeId.ToString() + assetInfo.AId.ToString();
            _air.AddInfo(assetInfo);//新增一条资产记录
            var assetRecord = _mapper.Map<AssetModificationRecord>(addassetInfoDTO);
            assetRecord.AdminId = _tokenService.GetUId();
            assetRecord.AdminIdName = _tokenService.GetUserName();
            assetRecord.RecordTime = DateTime.Now;
            assetRecord.AllId = assetInfo.AllId;
            assetRecord.assetModificationTypeId = (await _amr.GetReadOnlyInfoAsync(item => item.AMTName == "新增")).AMTId;
            _amrr.AddInfo(assetRecord);//新增一条资产操作记录
            var result = await _mysqlUnitOfWork.SaveAsync();//执行事务
            return result == 2
                ? true
                : await _mysqlUnitOfWork.DataRollback("增加失败,数据出现异常");//等于其他值说明出现了异常状况，回滚事务
        }


        public async Task<bool> DeleteAssetInfoAsync(int gid, int sid, int aid)
        {
            var assetInfo = await _air.GetReadOnlyInfoAsync(item => item.GeneralTypeId == gid && item.SecondaryTypeId == sid && item.AId == aid);//查询出要删除的数据
            if (assetInfo == null)
            {
                throw new ExceptionBO("要删除的资产信息不存在");
            }
            _air.DeleteInfo(assetInfo); //删除一条资产记录
            var assetRecord = _mapper.Map<AssetModificationRecord>(assetInfo);
            assetRecord.AdminId = _tokenService.GetUId();
            assetRecord.AdminIdName = _tokenService.GetUserName();
            assetRecord.RecordTime = DateTime.Now;
            assetRecord.assetModificationTypeId = (await _amr.GetReadOnlyInfoAsync(item => item.AMTName == "删除")).AMTId;
            _amrr.DeleteInfo(assetRecord);//删除一条资产操作记录
            var result = await _mysqlUnitOfWork.SaveAsync();//执行事务
            return result == 2
                ? true
                : await _mysqlUnitOfWork.DataRollback("删除失败，数据出现异常");//等于其他值说明出现了异常状况，回滚事务
        }

        public async Task<List<AssetInfoDTO>> GetAllAssetInfoAsync(int page, int num)
        {
            var assetInfos = await _air.GetReadOnlyInfoAsyncs<string>(page, num, "AllId");
            return assetInfos.Count == 0
                ? throw new ExceptionBO("未查询到任何数据")
                : _mapper.Map<List<AssetInfoDTO>>(assetInfos);

        }

        public async Task<List<AssetInfoDTO>> GetFuzzyAssetInfoAsync(string value, int page, int num)
        {
            var assetInfos = string.IsNullOrEmpty(value)
                ? await _air.GetReadOnlyInfoAsyncs<string>(page, num, "AllId")
                : await _air.GetFuzzyAssetInfoAsync(value, page, num);
            return assetInfos.Count == 0
                ? throw new ExceptionBO("未查询到任何数据")
                : _mapper.Map<List<AssetInfoDTO>>(assetInfos);
        }

        public async Task<bool> UpdateAssetInfoAsync(UpdateAssetInfoDTO updateAssetInfoDTO)
        {
            var assetInfo = _mapper.Map<AssetInfo>(updateAssetInfoDTO);
            _air.UpdateInfo(assetInfo);
            var assetRecord = _mapper.Map<AssetModificationRecord>(updateAssetInfoDTO);
            assetRecord.AdminId = _tokenService.GetUId();
            assetRecord.AdminIdName = _tokenService.GetUserName();
            assetRecord.RecordTime = DateTime.Now;
            assetRecord.assetModificationTypeId = (await _amr.GetReadOnlyInfoAsync(item => item.AMTName == "修改")).AMTId;
            assetRecord.AllId = assetInfo.AllId;
            _amrr.AddInfo(assetRecord);
            var result = await _mysqlUnitOfWork.SaveAsync();//执行事务
            return result == 2
                ? true
                : await _mysqlUnitOfWork.DataRollback("修改失败，数据出现异常");//等于其他值说明出现了异常状况，回滚事务
        }
    }
}
